// components/project/project.js
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
var app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        list: Array,
        show: Boolean,
        carLeft: Number,
        carBottom: Number
    },

    /**
     * 组件的初始数据
     */
    data: {
        height: app.globalData.height * 2 + 20,
        lunbo: [],
        hide_good_box: true, // 抛物线动画
        num: 0,
        imgUrl: config.imgUrl,
    },
    lifetimes: {
        attached() {
            console.log('carLeft', this.data.carLeft)
            console.log('carBottom', this.data.carBottom)
        },
        detached() {
            // 在组件实例被从页面节点树移除时执行
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        toVideo(e) {
            var id = e.target.dataset.id
            wx.navigateTo({
                url: "/pages/video/video?id=" + id
            })
        },
        // 点击添加菜谱
        add(e) {
            var listMenus = wx.getStorageSync('select-menus') || [];
            for (let v of listMenus) {
                if (v.id == e.target.dataset.id) {
                    wx.showToast({
                        title: '请不要重复添加',
                        icon: 'none',
                        duration: 1500
                    })
                    return false;
                }
            }
            var newList = wx.getStorageSync("select-menus") || []
            newList.filter((item) => {
                return item.num
            })
            var count = 0
            newList.map((item) => {
                count += item.num
            })
            var num = count + 1
            this.triggerEvent('myevent', { num });


            listMenus.push({
                flag: true,
                id: e.target.dataset.id,
                name: e.target.dataset.name,
                main_img_url: e.target.dataset.img,
                ingredients: [],
                count: e.target.dataset.count,
                time: e.target.dataset.time,
                seasoning_count: e.target.dataset.seasoning_count,
                num: 1
            });
            wx.setStorageSync("select-menus", listMenus);
            wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 1500
            })
            this.busPos = {};
            this.busPos['x'] = this.data.carLeft;
            this.busPos['y'] = this.data.carBottom - 20;
            console.log('菜篮的位置', this.busPos);


            console.log('eeeeeeeeee', e)
                // 如果good_box正在运动
            if (!this.data.hide_good_box) return;
            this.finger = {};
            var topPoint = {};
            this.finger['x'] = e.touches["0"].clientX;
            this.finger['y'] = e.touches["0"].clientY;

            console.log('点击点的位置:', this.finger)




            // topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2 + this.finger['x'];


            // 控制点x控制在点击点和菜篮之间
            if (this.finger['x'] > this.busPos['x']) {
                console.log(11111111111)

                this.finger['x'] = e.touches["0"].clientX - 20;
                this.finger['y'] = e.touches["0"].clientY - 30;
                topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2 + this.finger['x'];
            } else {
                console.log(2222222222)
                topPoint['x'] = Math.abs(this.busPos['x'] - this.finger['x']) / 2 + this.finger['x'];
            }

            // 控制点的y坐标定在低的点的上方
            if (this.finger['y'] < this.busPos['y']) {
                topPoint['y'] = this.finger['y'];
            } else {
                topPoint['y'] = this.busPos['y'];
            }
            console.log('控制点的位置', topPoint)


            // 获得曲线的点
            this.linePos = app.bezier([this.finger, topPoint, this.busPos], 30);
            console.log('曲线的点', this.linePos);

            this.startAnimation();

        },
        startAnimation: function() {
            var index = 0,
                that = this,
                bezier_points = that.linePos['bezier_points'];
            this.setData({
                hide_good_box: false,
                bus_x: that.finger['x'],
                bus_y: that.finger['y']
            })
            this.timer = setInterval(function() {
                index++;
                that.setData({
                    bus_x: bezier_points[index]['x'],
                    bus_y: bezier_points[index]['y']
                })
                if (index >= 28) {
                    clearInterval(that.timer);
                    that.setData({
                        hide_good_box: true,
                    })
                }
            }, 33);


        },
    }
})